﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Inspection
{
    public class PersonnelInspections : DetailView<personnelinspection>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<personnelinspection> EntityList;
        private new personnelinspection Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<personnelinspection>

        public override List<personnelinspection> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.personnelinspections.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<personnelinspection> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.personnelinspections.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override personnelinspection GetEntity()
        {
            _context = new project5188Entities();
            Entity = new personnelinspection();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTopersonnelinspections(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<personnelinspection> GetEntityListByParameters(string personnelName, string registryNoText, bool? isInspectionEvent, DateTime? inspectionDate)
        {
            _context = new project5188Entities();
            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.personnelinfo.IsDeleted = @PersonnelIsDeleted";
            parameterList.Add(new ObjectParameter("PersonnelIsDeleted", false));

            if (!string.IsNullOrEmpty(personnelName) && !string.IsNullOrWhiteSpace(personnelName))
            {
                dynamicString += " And it.personnelinfo.f_NameSurname = @PersonnelName";
                parameterList.Add(new ObjectParameter("PersonnelName", personnelName));
            }

            if (!string.IsNullOrEmpty(registryNoText) && !string.IsNullOrWhiteSpace(registryNoText))
            {
                dynamicString += " And it.personnelinfo.b_RegisterNo = @RegisterNo";
                parameterList.Add(new ObjectParameter("RegisterNo", Convert.ToInt32(registryNoText)));
            }

            if (isInspectionEvent != null)
            {
                dynamicString += " And it.c_IsInspectionEvent = @IsInspectionEvent";
                parameterList.Add(new ObjectParameter("IsInspectionEvent", isInspectionEvent));
            }

            if (inspectionDate != null)
            {
                dynamicString += " And it.f_InspectionDate = @InspectionDate";
                parameterList.Add(new ObjectParameter("InspectionDate", inspectionDate));
            }

            EntityList = _context.personnelinspections.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
